package engine.systems.motion.rotateToVelocity
{
	import engine.classes.enumerations.MovementType;
	import engine.classes.enumerations.SystemPriority;
	import engine.groups.Group;
	import engine.systems.update.GameSystem;
	import engine.utils.MathUtils;
	
	public class RotateToVelocitySystem extends GameSystem
	{
		private var degrees:Number;
		private var delta:Number;
		
		public function RotateToVelocitySystem()
		{
			super(RotateToVelocityNode, updateNode);
			
			this.priority = SystemPriority.PRE_MOTION;
		}
		
		public function updateNode(node:RotateToVelocityNode, time:Number):void
		{
			degrees = Math.atan2(node.motion.velocityY, node.motion.velocityX);
			degrees = MathUtils.toDegrees(degrees) + node.rotate.offset;
			
			switch(node.rotate.movement)
			{
				case MovementType.SPATIAL:
					node.spatial.rotation = degrees;
					break;
				
				case MovementType.VELOCITY:
					delta = MathUtils.toRotation(degrees - node.spatial.rotation);
					node.motion.velocityRotation = delta * node.rotate.rate;
					break;
				
				case MovementType.ACCELERATION:
					delta = MathUtils.toRotation(degrees - node.spatial.rotation);
					node.motion.accelerationRotation = delta * node.rotate.rate;
					break;
			}
		}
	}
}